<template>
    <el-dialog
            :title="title"
            :visible.sync="dialogVisible"
            width="30%"
            :before-close="handleClose">
        <div>
            <el-form
                    :rules="loginRules"
                    ref="form" :model="form" label-width="80px">

                <el-form-item :label="key" :prop="key" disabled="false" v-for="(value,key) in form">
                    <el-input v-model="form[key]" :disabled="false"></el-input>
                </el-form-item>


            </el-form>
        </div>
        <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="()=>{
   submit()
    }">确 定</el-button>
  </span>
    </el-dialog>

</template>
<script>

    import {getCookie, getUrlParamNative} from "../../utils/utils";
import  lodash from 'lodash'
    export default {
        data() {
            return {
                tableName:"",
                title: "管理",
                dialogVisible: false,
                form: {},
                loginRules: {},
                isUpdate:false
            }
        },
        methods: {
            handleClose() {
                this.dialogVisible=false
            },
            init(_data,isUpdate=false) {
                this.isUpdate=isUpdate
                this.tableName=getUrlParamNative('table')
                if (_data && _data.id) {
                    this.title = "修改"
                } else {
                    this.title = "添加"
                }
                this.dialogVisible = true
                // debugger
                //设置一下value
                var __data={}
                lodash.map(_data,(value,key)=>{
                    if(!value){
                        __data[key]=''
                    }
                    else if(typeof (value)=='string'){
                        __data[key]=value
                    }else if(typeof (value)=='number'){
                        __data[key]=value
                    } else if(typeof (value)=='object'){
                        __data[key]=value.value
                    }

                })
                console.log("弹出框 初始化data",__data)
                this.form = __data ? __data : {}
            },
            async submit() {
                var _params = this.form
                _params.token=getCookie('token')
                console.log("[get]  /${this.tableName}/xxx")
                console.log('参数', JSON.stringify(_params))
                var url = this.isUpdate ? `/mock/${this.tableName}/update` : `/mock/${this.tableName}/add`
                var result = await axios.post(url, _params).then(res => res.data)
                console.log("结果", result)
                this.dialogVisible = false;
                this.$emit('refresh')
            }
        }
    }
</script>

